package com.century.sjt.activity.Topic;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.century.sjt.R;
import com.century.sjt.activity.BaseActivity;
import com.century.sjt.photoview.PhotoViewAttacher;
import com.century.sjt.util.Constant;
import com.century.sjt.util.FileUtil;
import com.century.sjt.widget.TitleBar.SjtTitleBar;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


public class ImageShowActivity extends BaseActivity {
    private SjtTitleBar titleBar;
    private ImageButton btnSys;                                     //返回按钮
    private TextView mTitle;                                        //程序标题
    private ImageButton btnInfo;                                    //右边按钮
    private LinearLayout leftLinLayout;                             //左边的线性布局
//    private NetworkImageView imageView;                             //图片
    private ImageView imageView;

    private RequestQueue mImgShowReque;                             //图片序列

    private PhotoViewAttacher mAttacher;

    private ProgressDialog progressDialog;                          //加载滚动条
    private RequestQueue mQueue;                                    //请求序列
    private Handler mHandler;

    private String imgid;                                           //图片名字

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_show);

        mImgShowReque= Volley.newRequestQueue(this);

        titleBar = (SjtTitleBar) findViewById(R.id.title_bar);
        //       标题栏
        titleBar.setLeftLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        titleBar.setTitle(getResources().getString(R.string.tab_picture_preview));
//        mTitle= (TextView) findViewById(R.id.img_title);
//        mTitle.setText("图片预览");
//        btnSys= (ImageButton) findViewById(R.id.btn_sys);
////        btnSys.setImageResource(R.drawable.selector_btn_return);
//        btnSys.setClickable(false);
//        btnInfo= (ImageButton) findViewById(R.id.btn_information);
//
//        leftLinLayout= (LinearLayout) findViewById(R.id.leftLinLayout);
//        leftLinLayout.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                finish();
//                overridePendingTransition(R.animator.in_from_left, R.animator.out_to_right);
//            }
//        });

//        imageView= (NetworkImageView) findViewById(R.id.isa_image);

        mQueue = Volley.newRequestQueue(this);
        imageView = (ImageView) findViewById(R.id.isa_image);
        mAttacher = new PhotoViewAttacher(imageView);

        Intent intent=getIntent();
        imgid=intent.getStringExtra("imgid");
        if(imgid!=null&&!imgid.equals("")){
            //读取图片
//            final String imgUrl= Constant.ImageHost_Big + imgid;
//            LruImageCache lruImageCache=LruImageCache.instance();
//            ImageLoader mImageLoader=new ImageLoader(mImgShowReque,lruImageCache);
//            imageView.setImageUrl(imgUrl, mImageLoader);
            //启动滚动条
            progressDialog=ProgressDialog.show(this, getResources().getString(R.string.app_name), getResources().getString(R.string.xlistview_picture_hint_ready), false, false);
            progressDialog.setCancelable(true);
            if (!"".equals(imgid)) {
                FileUtil.setupBigImage(imgid, mQueue, imageView,mAttacher,progressDialog,ImageShowActivity.this);
            }


//            ImageRequest imageRequest = new ImageRequest(imgUrl, new Response.Listener<Bitmap>() {
//                @Override
//                public void onResponse(Bitmap bitmap) {
//                    saveFriendImg(bitmap, imgid);                //保存图片
//                    progressDialog.dismiss();
//                    //要求更新ImageView
//                    Message message = new Message();
//                    message.what = 0;
//                    mHandler.sendMessage(message);
//                }
//            }, 0, 0, ImageView.ScaleType.CENTER_CROP, Bitmap.Config.RGB_565, new Response.ErrorListener() {
//                @Override
//                public void onErrorResponse(VolleyError volleyError) {
//                    progressDialog.dismiss();
//                    Toast.makeText(ImageShowActivity.this, "读取图片时发生错误！" + volleyError.getLocalizedMessage(), Toast.LENGTH_LONG).show();
//                }
//            });
//            mQueue.add(imageRequest);

        }

        mHandler =  new Handler(){
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what){
                    case 0:                                             //更新图片
                        String localImgUrl= Constant.CAMERA_PICTURE_PATH+imgid;
                        Bitmap bitmap = BitmapFactory.decodeFile(localImgUrl);
                        imageView.setImageBitmap(bitmap);

                        mAttacher.update();
                        break;
                }
            }
        };

    }

    public static void saveFriendImg(Bitmap bitmap,String friendImageId){                            //保存用户头像到指定文件夹
        if(!friendImageId.equals("")) {
            File f = new File(Constant.CAMERA_PICTURE_PATH, friendImageId);
            if (f.exists()) {
                f.delete();
            }
            try{
                FileOutputStream out = new FileOutputStream(f);
                bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
                out.flush();
                out.close();
//                Log.e("sjt","保存照片至sd卡成功！");
            }catch (FileNotFoundException e){
                e.printStackTrace();
            }catch (IOException e){
                e.printStackTrace();
            }
//            Log.e("sjt","结束保存图片！");
        }
    }

    @Override
    protected void onStop() {
        if(mImgShowReque!=null)
            mImgShowReque.cancelAll(this);
        super.onStop();
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected void initData() {

    }

    //    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        // Inflate the menu; this adds items to the action bar if it is present.
//        getMenuInflater().inflate(R.menu.menu_image_show, menu);
//        return true;
//    }
//
//    @Override
//    public boolean onOptionsItemSelected(MenuItem item) {
//        // Handle action bar item clicks here. The action bar will
//        // automatically handle clicks on the Home/Up button, so long
//        // as you specify a parent activity in AndroidManifest.xml.
//        int id = item.getItemId();
//
//        //noinspection SimplifiableIfStatement
//        if (id == R.id.action_settings) {
//            return true;
//        }
//
//        return super.onOptionsItemSelected(item);
//    }
}
